package com.dm.adapter;

import static com.dm.DmActivity.alAlarm;
import static com.dm.DmActivity.currentPosition;
import static com.dm.DmActivity.wcNewOrEdit;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dm.R;
import com.dm.bean.Schedule;
import com.dm.util.Constant.WhoCall;
import com.google.ads.e;

public class ListViewAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Schedule> list;
    private LayoutInflater inflater = null;
    private Handler handler;

    public ListViewAdapter(Context context, ArrayList<Schedule> list, Handler handler) {
	this.context = context;
	this.list = list;
	this.handler = handler;
	inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
	return list.size();
    }

    @Override
    public Object getItem(int position) {
	return list.get(position);
    }

    @Override
    public long getItemId(int position) {
	return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
	ViewHolder holder = null;
	if (convertView == null) {
	    convertView = inflater.inflate(R.layout.item_list, null);
	    holder = new ViewHolder();
	    holder.llBG = (LinearLayout) convertView.findViewById(R.id.llBG);
	    holder.llDate = (LinearLayout) convertView.findViewById(R.id.llDate);
	    holder.llBtn = (LinearLayout) convertView.findViewById(R.id.llBtn);
	    holder.tvDate1 = (TextView) convertView.findViewById(R.id.tvDate1);
	    holder.tvDate2 = (TextView) convertView.findViewById(R.id.tvDate2);
	    holder.tvDate3 = (TextView) convertView.findViewById(R.id.tvDate3);
	    holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
	    holder.tvWeek = (TextView) convertView.findViewById(R.id.tvWeek);
	    holder.iv = (ImageView) convertView.findViewById(R.id.iv);
	    holder.tvType = (TextView) convertView.findViewById(R.id.tvType);
	    holder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
	    holder.tvNote = (TextView) convertView.findViewById(R.id.tvNote);
	    holder.tvAlarm = (TextView) convertView.findViewById(R.id.tvAlarm);
	    holder.btnEdit = (Button) convertView.findViewById(R.id.btnEdit);
	    holder.btnDel = (Button) convertView.findViewById(R.id.btnDel);
	    convertView.setTag(holder);
	} else {
	    holder = (ViewHolder) convertView.getTag();
	}
	holder.tvTime.setText(list.get(position).timeForListView());
	// 若日程已過期，則日期和時間顏色、背景色設置為過期的顏色
	boolean pass = list.get(position).isPassed();
	if (pass) {
	    holder.tvDate3.setTextColor(Color.RED);
	    holder.llBG.setBackgroundResource(R.drawable.shape_line_pass);
	    holder.tvTitle.setTextColor(Color.WHITE);
	    holder.tvAlarm.setTextColor(Color.WHITE);
	    holder.tvNote.setTextColor(Color.WHITE);
	} else {
	    holder.tvDate3.setTextColor(Color.BLACK);
	    holder.llBG.setBackgroundResource(R.drawable.shape_line);
	    holder.tvTitle.setTextColor(Color.BLACK);
	    holder.tvAlarm.setTextColor(Color.BLACK);
	    holder.tvNote.setTextColor(Color.BLACK);
	}
	String[] Date = list.get(position).getDate1().split("/");
	holder.tvDate1.setText(Date[0]);
	holder.tvDate2.setText(Date[1]);
	holder.tvDate3.setText(Date[2]);
	int week = getWeekDay(Date[0], Date[1], Date[2]);
	switch (week) {
	    case 0:
		holder.tvWeek.setText("週一");
		if (position == currentPosition) {
		    holder.llDate.setBackgroundResource(R.drawable.shape_line_1_c);
		} else {
		    holder.llDate.setBackgroundResource(R.drawable.shape_line_1);
		}
		break;
	    case 1:
		holder.tvWeek.setText("週二");
		if (position == currentPosition) {
		    holder.llDate.setBackgroundResource(R.drawable.shape_line_2_c);
		} else {
		    holder.llDate.setBackgroundResource(R.drawable.shape_line_2);
		}
		break;
	    case 2:
		holder.tvWeek.setText("週三");
		if (position == currentPosition) {
		    holder.llDate.setBackgroundResource(R.drawable.shape_line_3_c);
		} else {
		    holder.llDate.setBackgroundResource(R.drawable.shape_line_3);
		}
		break;
	    case 3:
		holder.tvWeek.setText("週四");
		if (position == currentPosition) {
		    holder.llDate.setBackgroundResource(R.drawable.shape_line_4_c);
		} else {
		    holder.llDate.setBackgroundResource(R.drawable.shape_line_4);
		}
		break;
	    case 4:
		holder.tvWeek.setText("週五");
		if (position == currentPosition) {
		    holder.llDate.setBackgroundResource(R.drawable.shape_line_5_c);
		} else {
		    holder.llDate.setBackgroundResource(R.drawable.shape_line_5);
		}
		break;
	    case 5:
		holder.tvWeek.setText("週六");
		if (position == currentPosition) {
		    holder.llDate.setBackgroundResource(R.drawable.shape_line_6_c);
		} else {
		    holder.llDate.setBackgroundResource(R.drawable.shape_line_6);
		}
		break;
	    case 6:
		holder.tvWeek.setText("週日");
		if (position == currentPosition) {
		    holder.llDate.setBackgroundResource(R.drawable.shape_line_7_c);
		} else {
		    holder.llDate.setBackgroundResource(R.drawable.shape_line_7);
		}
		break;
	}
	// 如果有鬧鐘，則加上鬧鐘的圖標
	if (list.get(position).getAlarmSet()) {
	    holder.iv.setImageDrawable(context.getResources().getDrawable(R.drawable.alarm_clock));
	    String time2 = list.get(position).getTime2();
	    for (int i = 0; i < alAlarm.size(); i++) {
		if (time2.equals(alAlarm.get(i).getValue())) {
		    holder.tvAlarm.setText(alAlarm.get(i).getText());
		}
	    }
	    holder.iv.setVisibility(View.VISIBLE);
	    holder.tvAlarm.setVisibility(View.VISIBLE);
	} else {
	    holder.iv.setVisibility(View.GONE);
	    holder.tvAlarm.setVisibility(View.GONE);
	}
	holder.tvType.setText(list.get(position).typeForListView());
	holder.tvTitle.setText(list.get(position).getTitle());
	holder.tvNote.setText(list.get(position).getNote());
	if (position == currentPosition) {
	    if (pass) {
		holder.llBG.setBackgroundResource(R.drawable.shape_line_pass_c);
	    } else {
		holder.llBG.setBackgroundResource(R.drawable.shape_line_c);
	    }
	    holder.llBtn.setVisibility(View.VISIBLE);
	    holder.btnEdit.setClickable(true);
	    holder.btnEdit.setOnClickListener(clickListener);
	    holder.btnDel.setClickable(true);
	    holder.btnDel.setOnClickListener(clickListener);
	} else {
	    if (pass) {
		holder.llBG.setBackgroundResource(R.drawable.shape_line_pass);
	    } else {
		holder.llBG.setBackgroundResource(R.drawable.shape_line);
	    }
	    holder.llBtn.setVisibility(View.GONE);
	    holder.btnEdit.setClickable(false);
	    holder.btnDel.setClickable(false);
	}
	return convertView;
    }

    OnClickListener clickListener = new OnClickListener() {

	@Override
	public void onClick(View v) {
	    switch (v.getId()) {
		case R.id.btnEdit:
		    wcNewOrEdit = WhoCall.EDIT;
		    currentPosition = -1;
		    handler.sendEmptyMessage(1);
		    break;
		case R.id.btnDel:
		    currentPosition = -1;
		    handler.sendEmptyMessage(2);
		    break;
	    }
	}
    };

    static class ViewHolder {

	LinearLayout llBG;
	LinearLayout llDate;
	LinearLayout llBtn;
	TextView tvDate1;
	TextView tvDate2;
	TextView tvDate3;
	TextView tvWeek;
	TextView tvTime;
	ImageView iv;
	TextView tvType;
	TextView tvTitle;
	TextView tvNote;
	TextView tvAlarm;
	Button btnEdit;
	Button btnDel;
    }

    private int getWeekDay(String year, String month, String day) {
	int y = Integer.parseInt(year);
	int m = Integer.parseInt(month);
	int d = Integer.parseInt(day);
	if (m == 1)
	    m = 13;
	if (m == 2)
	    m = 14;
	int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
	return week;
    }
}
